package com.xcdh.auth.util;



import cn.hutool.core.util.StrUtil;
import com.xcdh.commons.spring.SpringUtils;
import com.xcdh.domain.entity.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;

import java.util.Base64;

public class SecurityUtil {

    private final static String SLAT = "com.xcdh.auth";

    /**
     *
     * @param userDO
     * @param plainPassword 明文
     * @return
     */
    public static boolean verifyUser(UserDO userDO , String plainPassword){
        return passwordMatches(plainPassword , userDO.getPassword()) ;
    }

    /**
     *
     * @param source 加密内容
     * @param slat
     * @return
     */
    private static String passwordEncode(String source , String slat){
        String encode = Base64Utils.encodeToString((source + slat).getBytes());
        return DigestUtil.encrypt(encode);
    }

    public static boolean passwordMatches(String plainPassword , String passwordEncodeStr){
       String rawPassword = passwordEncode(plainPassword , SLAT) ;
       return StrUtil.equals(rawPassword , passwordEncodeStr) ;
    }

    public static void main(String[] args) {
        System.out.println(passwordEncode("123456" , SLAT));
    }

}
